package cn.ruituo.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@WebFilter(filterName = "aaa",displayName = "aaa",value = "/*")
public class TimeFiler implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter init ");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        long start=System.nanoTime();
        System.out.println("进入了时间过滤器");
        HttpServletRequest req=(HttpServletRequest)servletRequest;
        String path=req.getServletPath();//
        System.out.println("当前访问地址："+path);
        req.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");
        //放行
        filterChain.doFilter(req,servletResponse);
        long end=System.nanoTime();
        System.out.println(path+"请求花费了"+(end-start)+"纳秒");
        System.out.println("离开了时间过滤器");
    }

    @Override
    public void destroy() {
        System.out.println("Filter destroy ");
    }
}
